package pms.common.dao;

import pms.common.value.MasterValue;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

public class MasterDaoImpl implements MasterDao {
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}


	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public List<MasterValue> getMaster(String key) {
		final List<MasterValue> result = new ArrayList<MasterValue>();
		String sql = "SELECT master_id, master_value FROM tbl_master where master_group=?";
		
		jdbcTemplate.query(sql, new Object[]{key}, new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				MasterValue mv = new MasterValue();
				mv.setCode(rs.getInt("master_id"));
				mv.setValue(rs.getString("master_value"));
				result.add(mv);
			}
		});
		if(result.size()==0)
			return null;
		else
			return result;
	}

}
